#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int j = 0, n, q, rnk[N], par[N], ans[N];
struct edge {
  int start, end, w;
  bool operator>(const edge &x) const { return w > x.w; }
};
struct query {
  int k, v, id;
  bool operator>(const query &x) const { return k > x.k; }
};
vector<edge> e;
vector<query> qry;
int find(int x) { return par[x] = x == par[x] ? x : find(par[x]); }
void unite(int x, int y) {
  x = find(x), y = find(y);
  if (x != y) {
    par[x] = y;
    rnk[y] += rnk[x];
  }
}
int main() {
  cin >> n >> q;
  for (int i = 0; i <= n; ++i) {
    par[i] = i;
    rnk[i] = 1;
  }
  for (int i = 1; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    e.push_back({a, b, c});
  }
  for (int i = 1; i <= q; ++i) {
    int k, v;
    cin >> k >> v;
    qry.push_back({k, v, i});
  }

  sort(e.begin(), e.end(), greater<edge>());
  sort(qry.begin(), qry.end(), greater<query>());

  for (int i = 0; i < q; ++i) {
    while (j < n && e[j].w >= qry[i].k) {
      auto [u, v, w] = e[j];
      unite(u, v);
      j++;
    }
    ans[qry[i].id] = rnk[find(qry[i].v)];
  }
  for (int i = 1; i <= q; ++i) cout << ans[i] - 1 << endl;
  return 0;
}
